package com.qf.service.Impl;

import com.qf.constant.AppConstant;
import com.qf.enums.DevUserStateEnum;
import com.qf.enums.ResultEnum;
import com.qf.exception.AppException;
import com.qf.mapper.DevUserMapper;
import com.qf.pojo.DevUser;
import com.qf.service.DevUserService;
import com.qf.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import java.util.UUID;

@Service
@Slf4j
public class DevUserServiceImpl implements DevUserService {

    @Autowired
    DevUserMapper devUserMapper;

    @Autowired
    MailService mailService;

    @Override
    public Integer checkUsername(String username) {
        Integer count = devUserMapper.checkUsername(username);
        if (count>1){
            log.info("【异步校验用户名】 用户名存在多个 username = {}.",username);
        }

        return count;
    }

    @Override
    @Transactional

    public void register(DevUser devUser) throws MessagingException {

    devUser.setDevState(DevUserStateEnum.LOCK.getState());

    String salt= UUID.randomUUID().toString();
        String newPwd=new Md5Hash(devUser.getDevPassword(), salt, AppConstant.JIAMICISHU).toString();
        devUser.setDevPassword(newPwd);
        devUser.setDevSalt(salt);
        String code=System.currentTimeMillis()+UUID.randomUUID().toString();
        devUser.setDevCode(code);
        int count = devUserMapper.insertSelective(devUser);
        if (count!=1){
            log.error("[注册账号] 注册账号失败 devUser={}.",devUser);
            throw new AppException(ResultEnum.DEV_USER_REGISTER_ERROR);
        }


        sendActiveMail(devUser);
        //<a href="http://localhost/dev/user/active?devUsername=%s&code=%s">激活账号</a>

    }

    private void sendActiveMail(DevUser devUser) throws MessagingException {
        String content=String.format("<a href='http://localhost/dev/user/active?devUsername=%s&code=%s'>激活账号</a>",
                devUser.getDevUsername(),devUser.getDevCode());
        mailService.sendSimpleMail(devUser.getDevEmail(), "APP管理平台,开发者用户激活账号", content);
    }

    @Override
    @Transactional
    public void active(String devUsername, String code) {
        DevUser param=new DevUser();
        param.setDevUsername(devUsername);
        param.setDevCode(code);
        DevUser devUser = devUserMapper.selectOne(param);
        if (devUser==null){
            log.info("[激活账号] 当前用户不存在或激活失败 param={}",param );
            throw new AppException(ResultEnum.DEV_USER_ACTIVE_ERROR);
        }
        devUser.setDevState(DevUserStateEnum.ACTIVE.getState());
        devUser.setDevCode(null);
        int count = devUserMapper.updateByPrimaryKey(devUser);
        if (count!=1){
            log.info("【激活账号】,当前激活路径已失效!!! param = {}.",param);
            throw new AppException(ResultEnum.DEV_USER_UPDATE_ERROR);
        }
    }

    @Override
    public void resendActiveMail(String devUsername) throws MessagingException {
        DevUser devUser=new DevUser();
        devUser.setDevUsername(devUsername);
        DevUser param = devUserMapper.selectOne(devUser);
        if (param==null){
            log.info("[重新发送邮件] 用户不存在 devUser={}",devUser );
            throw new AppException(ResultEnum.DEV_USER_NOT_EXISTS);
        }


        if (param.getDevState()==1||param.getDevCode()==null){
            log.info("[重新发送邮件] 用户 devUser={}",devUser );
            throw new AppException(ResultEnum.DEV_USER_INFO_EXISTS);
        }

        String content=String.format("<a href='http://localhost/dev/user/active?devUsername=%s&code=%s'>激活账号</a>",
                param.getDevUsername(),param.getDevCode());
        mailService.sendSimpleMail(param.getDevEmail(), "APP管理平台,开发者用户激活账号", content);
    }

    @Override
    public DevUser login(String devUsername, String devPassword) {
        DevUser param=new DevUser();
        param.setDevUsername(devUsername);
        DevUser devUser = devUserMapper.selectOne(param);
        if (devUser==null){
            log.info("用户名错误",21 );
            throw new AppException(19,"用户名错误");
        }

        String newPwd=new Md5Hash(devPassword, devUser.getDevSalt(),1024).toString();
        if (!devUser.getDevPassword().equals(newPwd)){
            log.info("密码错误",20 );
            throw new AppException(19,"密码错误" );
        }

        if (!devUser.getDevUsername().equals(devUsername)){
            log.info("用户名错误",20 );
            throw new AppException(19,"用户名错误" );
        }

        return devUser;
    }
}
